package cn.gov.pbc.dream.controller;

import cn.gov.pbc.dream.common.Const;
import cn.gov.pbc.dream.common.ResponseCode;
import cn.gov.pbc.dream.common.ServerResponse;
import cn.gov.pbc.dream.pojo.UserSession;
import cn.gov.pbc.dream.pojo.query.bankfinancing.InfoQuery;
import cn.gov.pbc.dream.service.IBankFinancingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/bank-financing/")
@Slf4j
public class BankFinancingController {

    @Value("${upload_path}")
    private String uploadPath;

    @Autowired
    private IBankFinancingService iBankFinancingService;

    @PostMapping("upload.do")
    public ServerResponse upload(HttpSession session, @RequestParam("file") MultipartFile file) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        String destPath = session.getServletContext().getRealPath(this.uploadPath);
        if (file == null) {
            return ServerResponse.createByErrorMessage("文件上传失败");
        }
        return this.iBankFinancingService.uploadFile(session, file, destPath);
    }

    @PostMapping("getUnresolveMessageList.do")
    public ServerResponse getUnresolveMessageList(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingService.getUnresolveMessageList(session);
    }

    @DeleteMapping("deleteUpoladMessage.do/{filename}")
    public ServerResponse deleteUpoladMessage(HttpSession session, @PathVariable("filename") String filename) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingService.deleteUpoladMessage(session, filename);
    }

    @PostMapping("startResolveMessage.do")
    public ServerResponse startResolveMessage(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingService.startResolveMessage(session);
    }

    @PostMapping("checkResolveMessageStatus.do")
    public ServerResponse checkResolveMessageStatus(HttpSession session) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingService.checkResolveMessageStatus(session);
    }

    @PostMapping("getInfo.do")
    public ServerResponse getInfo(HttpSession session, @RequestBody InfoQuery query) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iBankFinancingService.getInfo(query);
    }

    @PostMapping("validateInfo.do/{month}")
    public ServerResponse validateInfo(HttpSession session, @PathVariable String month) {
        UserSession userSession = (UserSession) session.getAttribute(Const.CURRENT_USER);
        if (userSession == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return this.iBankFinancingService.validateInfo(month);
    }
}

